{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Conjugate Gradient Algorithm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "using LinearAlgebra\n",
    "using Optim"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solve\n",
    "$$\n",
    "\\min_x f(x) = \\frac{1}{2} x^T A x + b^T x + a\n",
    "$$\n",
    "where $A \\succ 0$. Setting $\\nabla f(x) = 0$, it is equivalent to solve the linear system $Ax = -b$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "#3 (generic function with 1 method)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f = x -> 0.5*dot(x,A*x)+dot(b,x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## A simple example\n",
    "\n",
    "Adapted from https://www.rose-hulman.edu/~bryan/lottamath/congrad.pdf\n",
    "\n",
    "Let\n",
    "$$\n",
    "A =\n",
    "\\begin{pmatrix}\n",
    "3 & 1 & 0 \\\\\n",
    "1 & 2 & 2 \\\\\n",
    "0 & 2 & 4\n",
    "\\end{pmatrix}\n",
    "$$\n",
    "Consider the function to minimize\n",
    "$$\n",
    "f(x) = \\frac{1}{2} x^TAx,\n",
    "$$\n",
    "and assume the we have already computed\n",
    "\\begin{align*}\n",
    "d_0 &= (1, 0, 0)\\\\\n",
    "d_1 & = (1, −3, 0)\\\\\n",
    "d_2 &= (−2, 6, −5).\n",
    "\\end{align*}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Check that $d_0$, $d_1$ and $d_2$ are $A$-conjugate."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0 0.0 0.0\n"
     ]
    }
   ],
   "source": [
    "A = [ 3.0 1 0 ; 1 2 2 ; 0 2 4]\n",
    "d0 = [ 1.0 0 0 ]'\n",
    "d1 = [ 1.0 -3.0 0.0 ]'\n",
    "d2 = [ -2.0 6.0 -5.0]'\n",
    "\n",
    "println(\"$(dot(d0, A*d1)) $(dot(d0, A*d2)) $(dot(d1, A*d2))\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(8.0, Eigen{Float64,Float64,Array{Float64,2},Array{Float64,1}}([0.47108204270564347, 3.167449191108536, 5.361468766185826], [-0.3253061660474964 0.9167566816845831 0.23180397951318818; 0.8226726049360933 0.153510164791429 0.5473978574979729; -0.4662463763067133 -0.3687706819859466 0.804128410571644]))"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "det(A), eigen(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Take initial guess $x_0 = (1, 2, 3)$. Compute $x_1$, $x_2$ and $x_3$ using the conjugate gradient algorithm. Is $x_3$ optimal?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "$$\n",
    "\\nabla f(x) = Ax\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3×1 Array{Float64,2}:\n",
       "  -5.0\n",
       " -11.0\n",
       " -16.0"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x0 = [1 2 3.0]'\n",
    "-A*x0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "#5 (generic function with 1 method)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f = x -> dot(x,A*x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We have to compute $\\alpha_k$, $k = 1,2,3$, by solving\n",
    "$$\n",
    "\\min_{\\alpha} f(x_k + \\alpha d_k)\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In order to obtain $\\alpha_0$, we have to minimize\n",
    "\\begin{align*}\n",
    "f(x_0 + \\alpha d_0) &= \\frac{1}{2}\n",
    "\\left(\\begin{pmatrix} 1 & 2 & 3\\end{pmatrix} + \\alpha \\begin{pmatrix} 1 & 0 & 0\\end{pmatrix} \\right)\n",
    "\\begin{pmatrix}\n",
    "3 & 1 & 0 \\\\\n",
    "1 & 2 & 2 \\\\\n",
    "0 & 2 & 4\n",
    "\\end{pmatrix}\n",
    "\\left(\\begin{pmatrix} 1 \\\\ 2 \\\\ 3 \\end{pmatrix} + \\alpha \\begin{pmatrix} 1 \\\\0 \\\\0 \\end{pmatrix} \\right)\n",
    "\\\\\n",
    "& = \\frac{1}{2}\\begin{pmatrix} 1 + \\alpha & 2 & 3 \\end{pmatrix}\n",
    "\\begin{pmatrix}\n",
    "3 & 1 & 0 \\\\\n",
    "1 & 2 & 2 \\\\\n",
    "0 & 2 & 4\n",
    "\\end{pmatrix}\n",
    "\\begin{pmatrix} 1 + \\alpha \\\\ 2 \\\\ 3 \\end{pmatrix}\\\\\n",
    "& = \\frac{1}{2}\\begin{pmatrix} 1 + \\alpha & 2 & 3 \\end{pmatrix}\n",
    "\\begin{pmatrix} 5+3\\alpha \\\\ 11+\\alpha \\\\ 16 \\end{pmatrix}\\\\\n",
    "& = \\frac{1}{2}\n",
    "((1 + \\alpha)(5+3\\alpha) + 22+2\\alpha + 48 ) \\\\\n",
    "& = \\frac{1}{2}\n",
    "( 3\\alpha^2 + 8\\alpha + 5 + 70 + 2\\alpha ) \\\\\n",
    "& = \\frac{3}{2}\\alpha^2 + 5\\alpha+\\frac{75}{2}\n",
    "\\end{align*}\n",
    "with respect to $\\alpha$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can obtain it by searching the zero of the derivative with respect to $\\alpha$, that is\n",
    "$$\n",
    "\\frac{d}{d\\alpha} f(x+\\alpha d) = 0\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "or"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\n",
    "d^T \\nabla f(x+\\alpha d) = 0\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Therefore, we must have"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\n",
    "3\\alpha + 5 = 0\n",
    "$$\n",
    "Thus\n",
    "$$\n",
    "\\alpha_{0} = -\\frac{5}{3}\n",
    "$$\n",
    "$$\n",
    "x_1 = x_0 - \\frac{5}{3} d_0 = \\begin{pmatrix} -\\frac{2}{3} \\\\ 2 \\\\ 3  \\end{pmatrix}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also directly compute $\\alpha_0$ as\n",
    "$$\n",
    "\\alpha_0 = - \\frac{d_0^T\\nabla f(x_0)}{d_0^TAd_0}\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "#7 (generic function with 1 method)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x0 = [1 ; 2 ; 3.0]\n",
    "∇f = x -> A*x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.6666666666666667"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d0 = [1 ; 0 ; 0]\n",
    "α0 = -dot(d0,∇f(x0))/dot(d0,A*d0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3-element Array{Float64,1}:\n",
       " -0.6666666666666667\n",
       "  2.0               \n",
       "  3.0               "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x1 = x0+α0*d0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A linesearch from $x_1$ in direction $d_1$ requires us to minimize\n",
    "$$\n",
    "f(x_1 + \\alpha d_1) = \\frac{15}{2}\\alpha^2 - 28\\alpha + \\frac{100}{3}\n",
    "$$\n",
    "which occurs at\n",
    "$$\n",
    "\\alpha_1 = \\frac{28}{15},\n",
    "$$\n",
    "yielding\n",
    "$$\n",
    "x_2 = x_1 + \\frac{28}{15}d_1 =\n",
    "    \\begin{pmatrix}\n",
    "     \\frac{6}{5} \\\\ \\frac{-18}{5} \\\\ 3\n",
    "    \\end{pmatrix}.\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.8666666666666665"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "α1 = -dot(d1,A*x1)/dot(d1,A*d1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.8666666666666667"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "28/15"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3×1 Array{Float64,2}:\n",
       "  1.1999999999999997\n",
       " -3.5999999999999996\n",
       "  3.0               "
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x2 = x1+α1*d1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The final linesearch from $x_2$ in direction $d_2$ requires us to minimize\n",
    "$$\n",
    "f(x_2 + \\alpha d_2) = 20 \\alpha^2 - 24\\alpha + \\frac{36}{5}\n",
    "$$\n",
    "which occurs at\n",
    "$$\n",
    "\\alpha_2 = \\frac{3}{5},\n",
    "$$\n",
    "yielding\n",
    "$$\n",
    "x_3 = x_2 + \\frac{3}{5}d_2 =\n",
    "    \\begin{pmatrix}\n",
    "     0 \\\\ 0 \\\\ 0\n",
    "    \\end{pmatrix},\n",
    "$$\n",
    "which is of course correct."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Similarly, we can compute the new point as"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3×1 Array{Float64,2}:\n",
       " -4.440892098500626e-16\n",
       "  8.881784197001252e-16\n",
       " -4.440892098500626e-16"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "α2 = -dot(d2,A*x2)/dot(d2,A*d2)\n",
    "x3 = x2+α2*d2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## A naive implementation\n",
    "\n",
    "A first version of the conjugate gradient algorithm follows."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "cg_quadratic (generic function with 2 methods)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "function cg_quadratic(A:: Matrix, b:: Vector, x0:: Vector, trace:: Bool = false)\n",
    "    n = length(x0)\n",
    "    x = x0\n",
    "    g = b+A*x\n",
    "    d = -g\n",
    "    if (trace)\n",
    "        iter = [ x ]\n",
    "        iterg = [ norm(g) ]\n",
    "        iterd = [ norm(d) ]\n",
    "    end\n",
    "    k = 0\n",
    "    \n",
    "    for k = 1:n-1\n",
    "        Ad = A*d\n",
    "        normd = dot(d,Ad)\n",
    "        α = -dot(d,g)/normd\n",
    "        x += α*d\n",
    "        if (trace)\n",
    "            iter = [ iter; [x] ]\n",
    "            iterg = [ iterg; norm(g)]\n",
    "            iterd = [ iterd; norm(d) ]\n",
    "        end\n",
    "        g = b+A*x\n",
    "        β = dot(g,Ad)/normd\n",
    "        d = -g+β*d\n",
    "    end\n",
    "\n",
    "    normd = dot(d,A*d)\n",
    "    α = -dot(d,g)/normd\n",
    "    x += α*d\n",
    "    if (trace)\n",
    "        g = b+A*x # g must be equal to 0\n",
    "        iter = [ iter; [x] ]\n",
    "        iterg = [ iterg; norm(g)]\n",
    "        iterd = [ iterd; norm(d) ]\n",
    "        return x, iter, iterg, iterd\n",
    "    end\n",
    "    \n",
    "    return x\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Consider the simple example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2-element Array{Float64,1}:\n",
       " -0.6666666666666666\n",
       "  0.3333333333333333"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = [2 1; 1 2]\n",
    "b = [1, 0]\n",
    "A\\(-b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solve\n",
    "$$\n",
    "    \\min_{\\alpha} f(x) = \\frac{1}{2}x^TAx+b^Tx+c\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or, equivalently, we solve\n",
    "$$\n",
    "    c+\\min_{\\alpha} f(x) = \\frac{1}{2}x^TAx+b^Tx\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([-0.6666666666666666, 0.3333333333333333], Array{Float64,1}[[0.0, 0.0], [-0.5, 0.0], [-0.6666666666666666, 0.3333333333333333]], [1.0, 1.0, 0.0], [1.0, 1.0, 0.5590169943749475])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cg_quadratic(A, b, [0, 0], true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What if $A$ is not positive definite?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2-element Array{Float64,1}:\n",
       "  0.3333333333333333\n",
       " -0.6666666666666666"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = [ 1 2 ; 2 1]\n",
    "A\\(-b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([0.33333333333333326, -0.6666666666666666], Array{Float64,1}[[0.0, 0.0], [-1.0, 0.0], [0.33333333333333326, -0.6666666666666666]], [1.0, 1.0, 1.1102230246251565e-16], [1.0, 1.0, 4.47213595499958])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cg_quadratic(A, b, [0, 0], true)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-3.0"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "det(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Eigen{Float64,Float64,Array{Float64,2},Array{Float64,1}}\n",
       "eigenvalues:\n",
       "2-element Array{Float64,1}:\n",
       " -1.0\n",
       "  3.0\n",
       "eigenvectors:\n",
       "2×2 Array{Float64,2}:\n",
       " -0.707107  0.707107\n",
       "  0.707107  0.707107"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eigen(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([0.3333333333333335, -0.6666666666666667], Array{Float64,1}[[1.0, 1.0], [-0.36986301369863006, -0.02739726027397249], [0.3333333333333335, -0.6666666666666667]], [5.0, 5.0, 2.220446049250313e-16], [5.0, 5.0, 0.9763790695367754])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cg_quadratic(A, b, [1, 1], true)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.3333333333333333"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f([1/3,-2/3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f([0,0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The conjugate gradient finds the solution of the linear system, and this does correspond to a first-order critical point of the function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "#9 (generic function with 1 method)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "∇f = x -> A*x+b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2-element Array{Float64,1}:\n",
       " 0.0\n",
       " 0.0"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = [1.0/3; -2.0/3]\n",
    "∇f(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2-element Array{Int64,1}:\n",
       " 4\n",
       " 3"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = [1; 1]\n",
    "∇f(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "#11 (generic function with 1 method)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "step= x -> x-α*∇f(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6886"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "α = 10\n",
    "dot(step(x),A*step(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Eigen{Float64,Float64,Array{Float64,2},Array{Float64,1}}\n",
       "eigenvalues:\n",
       "2-element Array{Float64,1}:\n",
       " -1.0\n",
       "  3.0\n",
       "eigenvectors:\n",
       "2×2 Array{Float64,2}:\n",
       " -0.707107  0.707107\n",
       "  0.707107  0.707107"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "λ, u = eigen(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2×2 Array{Float64,2}:\n",
       " -0.707107  0.707107\n",
       "  0.707107  0.707107"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-106.05992052357223"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = u[:,1]\n",
    "α = 10\n",
    "f = x -> 0.5*dot(x,A*x)+dot(b,x)\n",
    "f(step(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.417629483042249e6"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "α = 1000\n",
    "dot(step(x),A*step(x))+dot(b,x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.2071067811865475"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.16666666666666666"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = [1/3.0; -2/3]\n",
    "f(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([NaN, NaN], Array{Float64,1}[[0.3333333333333333, -0.6666666666666666], [NaN, NaN], [NaN, NaN]], [0.0, 0.0, NaN], [0.0, 0.0, NaN])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cg_quadratic(A, b, x, true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will need to incorporate a test on $\\nabla f(x_k)$!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Eigen{Float64,Float64,Array{Float64,2},Array{Float64,1}}\n",
       "eigenvalues:\n",
       "2-element Array{Float64,1}:\n",
       " 1.0\n",
       " 4.0\n",
       "eigenvectors:\n",
       "2×2 Array{Float64,2}:\n",
       " 1.0  0.5547 \n",
       " 0.0  0.83205"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = [ 1 2 ; 0 4 ]\n",
    "eigen(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Eigen{Float64,Float64,Array{Float64,2},Array{Float64,1}}\n",
       "eigenvalues:\n",
       "2-element Array{Float64,1}:\n",
       "  0.7917560805262003\n",
       " 20.2082439194738   \n",
       "eigenvectors:\n",
       "2×2 Array{Float64,2}:\n",
       " -0.885022  0.465549\n",
       "  0.465549  0.885022"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eigen(A*A')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Eigen{Float64,Float64,Array{Float64,2},Array{Float64,1}}\n",
       "eigenvalues:\n",
       "2-element Array{Float64,1}:\n",
       " 1.381966011250105\n",
       " 3.618033988749895\n",
       "eigenvectors:\n",
       "2×2 Array{Float64,2}:\n",
       "  0.525731  -0.850651\n",
       " -0.850651  -0.525731"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = [ 3 1; 1 2 ]\n",
    "eigen(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Eigen{Float64,Float64,Array{Float64,2},Array{Float64,1}}\n",
       "eigenvalues:\n",
       "2-element Array{Float64,1}:\n",
       "  1.9098300562505257\n",
       " 13.090169943749475 \n",
       "eigenvectors:\n",
       "2×2 Array{Float64,2}:\n",
       "  0.525731  -0.850651\n",
       " -0.850651  -0.525731"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eigen(A*A')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A more complex example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "500-element Array{Float64,1}:\n",
       " 0.0\n",
       " 0.0\n",
       " 0.0\n",
       " 0.0\n",
       " 0.0\n",
       " 0.0\n",
       " 0.0\n",
       " 0.0\n",
       " 0.0\n",
       " 0.0\n",
       " 0.0\n",
       " 0.0\n",
       " 0.0\n",
       " ⋮  \n",
       " 0.0\n",
       " 0.0\n",
       " 0.0\n",
       " 0.0\n",
       " 0.0\n",
       " 0.0\n",
       " 0.0\n",
       " 0.0\n",
       " 0.0\n",
       " 0.0\n",
       " 0.0\n",
       " 0.0"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = 500;\n",
    "m = 600;\n",
    "A = randn(n,m);\n",
    "A = A * A';  # A is now a positive semi-definite matrix\n",
    "A = A+I # A is positive definite\n",
    "b = zeros(n)\n",
    "for i = 1:n\n",
    "  b[i] = randn()\n",
    "end\n",
    "x0 = zeros(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "500-element Array{Float64,1}:\n",
       "  0.01597451883863155  \n",
       "  0.028709623797665867 \n",
       " -0.03192775432385789  \n",
       "  0.003984994205949193 \n",
       "  0.011428678456896275 \n",
       "  0.0027024152988841814\n",
       "  0.04200549972597002  \n",
       " -0.014099307479139152 \n",
       "  0.0021737037873165942\n",
       " -0.015045743623915846 \n",
       "  0.013122422505916118 \n",
       " -0.01759605391997169  \n",
       "  0.010265905433548106 \n",
       "  ⋮                    \n",
       " -0.006276648826344925 \n",
       "  0.010529080640319875 \n",
       "  0.02598928757621842  \n",
       "  0.023974826709166418 \n",
       " -0.005025111950753262 \n",
       " -0.016594864696717546 \n",
       "  0.008055002833526772 \n",
       " -0.01638615916647562  \n",
       "  0.02887626205712445  \n",
       "  0.00349453613595297  \n",
       "  0.011750890910482637 \n",
       "  0.035641000732530244 "
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b1 = A\\(-b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "b2, iter, iterg, iterd = cg_quadratic(A, b, x0, true);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.6153734771209275e-15"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "norm(b1-b2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "501-element Array{Float64,1}:\n",
       " 22.32669327290362      \n",
       " 22.32669327290362      \n",
       " 20.50902419138704      \n",
       " 18.944296782204336     \n",
       " 17.18987841717313      \n",
       " 15.301651241475364     \n",
       " 13.353113615228848     \n",
       " 12.00420404490857      \n",
       " 10.728004847403112     \n",
       "  9.683054723101787     \n",
       "  9.007965176527785     \n",
       "  7.881381957331188     \n",
       "  6.642753666018093     \n",
       "  ⋮                     \n",
       "  1.305385865226395e-13 \n",
       "  1.2093505031754848e-13\n",
       "  1.2456336870192955e-13\n",
       "  1.2068178785335202e-13\n",
       "  1.1894716127871671e-13\n",
       "  1.2428723655574613e-13\n",
       "  1.192953141223575e-13 \n",
       "  1.2271334977596007e-13\n",
       "  1.2388924678227773e-13\n",
       "  1.2191389512911638e-13\n",
       "  1.309232564317756e-13 \n",
       "  1.2340259757213947e-13"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iterg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "501-element Array{Float64,1}:\n",
       " 22.32669327290362      \n",
       " 22.32669327290362      \n",
       " 27.84853046480883      \n",
       " 30.388868610301273     \n",
       " 30.356845010726595     \n",
       " 28.50853006524143      \n",
       " 25.487983877133125     \n",
       " 23.841185386114986     \n",
       " 21.855554380639635     \n",
       " 20.267939081370205     \n",
       " 19.718196114080804     \n",
       " 17.028209112852267     \n",
       " 13.800435529001483     \n",
       "  ⋮                     \n",
       "  1.3472809490517593e-13\n",
       "  1.2561348952240697e-13\n",
       "  1.2720738284692763e-13\n",
       "  1.2631718703126367e-13\n",
       "  1.182164287161592e-13 \n",
       "  1.247567335462454e-13 \n",
       "  1.1953452841602304e-13\n",
       "  1.2460642630144355e-13\n",
       "  1.2872594443046195e-13\n",
       "  1.2869847664015946e-13\n",
       "  1.374659971495118e-13 \n",
       "  1.250566985805039e-13 "
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iterd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It works, but do we need to perform all the 500 iterations? We could be satisfied if we are close to the solution. We can mesure the residual of the linear system\n",
    "$$\n",
    "r = b+Ax\n",
    "$$\n",
    "that is nothing else the the gradient of the objective function of the quadratic optimization problem."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "501-element Array{Array{Float64,1},1}:\n",
       " [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]                                                                                                                                                                                                                                                                                                                                                                     \n",
       " [0.0002767606413135444, 0.0002733315138966624, -0.0010957482140589786, 0.00342103697035908, 0.0018584951036187565, 0.0017303923802678986, 0.0035685063965333794, 0.0015940332391309, 0.0016510508412575953, -0.0015997227786220224  …  0.0007459027881113781, 0.0015531034664322016, 0.0009448506398675648, 0.002238689009968792, 0.00021310327369990277, -0.00274278288496763, 0.0009802712223104066, 0.0019186132055392412, -0.0017158136204781266, 0.0018716526652683898]\n",
       " [0.0021490714058153616, 0.0007629911335404931, -0.0031487691334881555, 0.004438323091031676, 8.461420279218929e-5, 0.0009585810818215476, 0.007118534356608936, 0.004012831114951788, 0.004918817954130652, -0.0011926408558385236  …  0.0024887824385014115, 0.0042921185842997416, 0.00010360344224797323, 0.005607091027489634, -0.0019059984484024674, -0.00396819639417248, 0.0034418644468999604, 0.001739188721377177, 0.0011171072550803815, 0.005064177914187501]  \n",
       " [0.003195408904506128, 0.002235608465931285, -0.006802510376186047, 0.004226235401652949, -0.002084422577603974, 0.002525141905900659, 0.01068947848236571, 0.006163392293857908, 0.00495988129818389, -0.004090280013229431  …  0.005240095184622366, 0.006245936373706425, -0.0009275616626673143, 0.006368938798540335, -0.003141704735710403, -0.004170870460257801, 0.0054366044542427695, 0.0033573766331456934, 0.003086241176473166, 0.008217979576314467]          \n",
       " [0.0055819836434796944, 0.003782876929285174, -0.011619352274909536, 0.004418684309047652, -0.005489538173508283, 0.0016217230573770357, 0.01272324985700646, 0.006632667096692126, 0.003889295464481899, -0.004196336580523809  …  0.008406226658356683, 0.009738403865973709, -0.0010204960658463044, 0.00575332240220151, -0.004387303793255813, -0.00412565695269095, 0.008081736382745279, 0.0033443569002958567, 0.004016298985355728, 0.011198412913094646]          \n",
       " [0.006127340511896148, 0.0052123136400354635, -0.015532640355710666, 0.00478939446916952, -0.006439170956838267, 0.002078300885266658, 0.01499333642714634, 0.006025935561371879, 0.005249976040553713, -0.005209104979115552  …  0.01186265333454708, 0.011565703595667201, -0.0035977023217498997, 0.005355528011994289, -0.0038635735350661072, -0.004008781279297959, 0.009922578738427335, 0.003497103004682644, 0.006538384014633998, 0.013701118714159001]           \n",
       " [0.006264250363974774, 0.007610253579805663, -0.01898802214636052, 0.004573462684517747, -0.00650201931253576, 0.003108210392634197, 0.016378455587643323, 0.005373038127584463, 0.00631176429150276, -0.005729740451267855  …  0.014456722499981527, 0.011743740675506164, -0.005555609845850955, 0.0032244067091242745, -0.0031798031258372292, -0.004361940780360323, 0.011572102607867757, 0.004388619680806403, 0.006975448740538786, 0.01571688612814225]             \n",
       " [0.00733749249315043, 0.009762075665764158, -0.02278676250845416, 0.004556808165397187, -0.005321569960321089, 0.0025311683151397323, 0.01767958553941636, 0.003578276196607625, 0.0072160253034288585, -0.005583156139243623  …  0.015303803903663978, 0.012642879404798395, -0.005602308059261469, 0.0006600199783291377, -0.002291275591203592, -0.005821508879413295, 0.012816528882310097, 0.005213106400682633, 0.007617829404852674, 0.01672089328059665]            \n",
       " [0.008451900212033762, 0.012376672543492437, -0.026409406296086492, 0.004706278536736337, -0.0038940973783032793, 0.0007201184281646809, 0.020780982225419426, 0.0014582912737235083, 0.007685416917959291, -0.00579172207844359  …  0.015573084774772306, 0.013756216992174676, -0.004958781356837383, -0.0007404764905234711, -0.0006604737171881482, -0.007762843952430909, 0.013043521366959004, 0.0047746650775821855, 0.009084826598873743, 0.01885420963107185]      \n",
       " [0.010190973627825535, 0.013740562432625262, -0.027790782799994036, 0.004908253603424635, -0.0023192419293393443, -0.00022170927949265108, 0.02291034264263342, -0.0007108329940736079, 0.00708034707607827, -0.005742172709935214  …  0.01571114557410127, 0.01389092345907099, -0.0046887756759361994, -0.0014178201344966336, 0.0017451497002121978, -0.009280535300231336, 0.013912147361389623, 0.004445759045476112, 0.01055401623138824, 0.021515669685922927]       \n",
       " [0.012410272052588961, 0.015432816439064339, -0.028454074618211547, 0.004710451602986539, -0.00037806603169923876, -0.0009456370879246548, 0.024855427486849397, -0.0040047913625657, 0.007386284325693096, -0.0064692846969646774  …  0.016435899528826386, 0.015161502288416619, -0.003852305208918045, -0.003592960313085257, 0.0034306297206714853, -0.010276573327293105, 0.015027702150695708, 0.00467189133846526, 0.011911174055620668, 0.02317869289247129]        \n",
       " [0.013912445397753808, 0.01742352159454251, -0.029562670522507748, 0.005148719338287503, 0.0014585896715988349, -0.0013782440570599417, 0.026351763783559627, -0.0060267119383082945, 0.007236075860697661, -0.007573467931412091  …  0.016818055759604152, 0.017009827398849403, -0.00325502977560344, -0.005552018028596752, 0.005221264609027332, -0.010456511639477313, 0.01682769039086509, 0.004501862116846576, 0.013502087893792316, 0.02539842143579002]           \n",
       " [0.014645451431577602, 0.019284836361711254, -0.030456030288169265, 0.005468758545884511, 0.00340032494479259, -0.0016231344799777832, 0.028188635313930614, -0.007094131870917807, 0.006995949930586743, -0.008486433281395665  …  0.01741807221734211, 0.018357826350747064, -0.0028834431408075057, -0.006597175465300477, 0.005842468498256598, -0.010553663814008763, 0.01901233848418916, 0.004445748498591911, 0.014842933254498812, 0.026868188969128844]           \n",
       " ⋮                                                                                                                                                                                                                                                                                                                                                                                                                                                                           \n",
       " [0.01597451883863156, 0.028709623797665892, -0.03192775432385786, 0.003984994205949083, 0.011428678456896355, 0.0027024152988841914, 0.04200549972597005, -0.014099307479139126, 0.002173703787316619, -0.015045743623915805  …  0.02598928757621854, 0.023974826709166522, -0.0050251119507532796, -0.016594864696717528, 0.008055002833526816, -0.01638615916647567, 0.02887626205712443, 0.003494536135952922, 0.011750890910482608, 0.035641000732530397]               \n",
       " [0.015974518838631557, 0.02870962379766589, -0.03192775432385786, 0.003984994205949087, 0.011428678456896352, 0.0027024152988841814, 0.04200549972597005, -0.014099307479139123, 0.002173703787316616, -0.015045743623915796  …  0.025989287576218534, 0.023974826709166522, -0.005025111950753268, -0.016594864696717525, 0.008055002833526807, -0.016386159166475674, 0.028876262057124426, 0.0034945361359529307, 0.011750890910482613, 0.035641000732530397]            \n",
       " [0.01597451883863156, 0.0287096237976659, -0.03192775432385786, 0.003984994205949094, 0.011428678456896353, 0.002702415298884181, 0.04200549972597006, -0.014099307479139112, 0.002173703787316605, -0.015045743623915786  …  0.025989287576218538, 0.02397482670916653, -0.0050251119507532666, -0.016594864696717518, 0.00805500283352681, -0.01638615916647568, 0.02887626205712444, 0.0034945361359529337, 0.011750890910482615, 0.03564100073253041]                   \n",
       " [0.015974518838631564, 0.0287096237976659, -0.03192775432385786, 0.003984994205949094, 0.011428678456896345, 0.00270241529888418, 0.04200549972597006, -0.014099307479139109, 0.0021737037873165947, -0.01504574362391579  …  0.02598928757621855, 0.023974826709166532, -0.005025111950753265, -0.016594864696717514, 0.00805500283352681, -0.016386159166475685, 0.028876262057124437, 0.0034945361359529324, 0.011750890910482613, 0.035641000732530424]                 \n",
       " [0.015974518838631567, 0.028709623797665902, -0.03192775432385787, 0.003984994205949089, 0.011428678456896341, 0.002702415298884181, 0.04200549972597006, -0.014099307479139109, 0.0021737037873165795, -0.015045743623915793  …  0.025989287576218552, 0.02397482670916654, -0.005025111950753257, -0.016594864696717507, 0.008055002833526807, -0.01638615916647568, 0.028876262057124413, 0.0034945361359529376, 0.011750890910482615, 0.03564100073253043]              \n",
       " [0.015974518838631567, 0.0287096237976659, -0.03192775432385788, 0.003984994205949086, 0.011428678456896348, 0.0027024152988841784, 0.042005499725970064, -0.014099307479139116, 0.0021737037873165795, -0.015045743623915791  …  0.025989287576218552, 0.023974826709166553, -0.005025111950753256, -0.0165948646967175, 0.008055002833526802, -0.01638615916647568, 0.02887626205712441, 0.0034945361359529363, 0.011750890910482618, 0.03564100073253042]                \n",
       " [0.015974518838631564, 0.028709623797665878, -0.031927754323857895, 0.003984994205949095, 0.011428678456896347, 0.002702415298884179, 0.042005499725970064, -0.014099307479139126, 0.002173703787316585, -0.015045743623915798  …  0.025989287576218555, 0.023974826709166557, -0.005025111950753251, -0.016594864696717507, 0.0080550028335268, -0.016386159166475688, 0.028876262057124406, 0.0034945361359529285, 0.011750890910482616, 0.035641000732530424]            \n",
       " [0.015974518838631557, 0.028709623797665878, -0.031927754323857895, 0.003984994205949096, 0.011428678456896348, 0.002702415298884177, 0.04200549972597007, -0.014099307479139133, 0.0021737037873165864, -0.015045743623915801  …  0.02598928757621856, 0.02397482670916655, -0.005025111950753245, -0.016594864696717514, 0.008055002833526802, -0.016386159166475688, 0.028876262057124402, 0.003494536135952923, 0.011750890910482615, 0.03564100073253042]              \n",
       " [0.015974518838631557, 0.028709623797665885, -0.031927754323857895, 0.003984994205949102, 0.011428678456896353, 0.0027024152988841797, 0.042005499725970064, -0.014099307479139124, 0.0021737037873165847, -0.015045743623915798  …  0.025989287576218566, 0.02397482670916656, -0.005025111950753246, -0.016594864696717518, 0.008055002833526805, -0.016386159166475685, 0.028876262057124402, 0.0034945361359529246, 0.01175089091048262, 0.0356410007325304]            \n",
       " [0.015974518838631557, 0.028709623797665867, -0.031927754323857895, 0.003984994205949112, 0.011428678456896362, 0.0027024152988841814, 0.04200549972597007, -0.014099307479139116, 0.002173703787316591, -0.015045743623915794  …  0.02598928757621857, 0.02397482670916657, -0.005025111950753252, -0.016594864696717528, 0.008055002833526807, -0.016386159166475685, 0.028876262057124423, 0.003494536135952923, 0.011750890910482615, 0.03564100073253039]              \n",
       " [0.01597451883863156, 0.028709623797665854, -0.031927754323857895, 0.00398499420594912, 0.011428678456896367, 0.002702415298884185, 0.04200549972597007, -0.014099307479139121, 0.0021737037873165977, -0.015045743623915796  …  0.025989287576218583, 0.02397482670916658, -0.005025111950753255, -0.01659486469671753, 0.008055002833526805, -0.016386159166475685, 0.028876262057124433, 0.00349453613595292, 0.01175089091048261, 0.03564100073253038]                  \n",
       " [0.015974518838631564, 0.02870962379766585, -0.03192775432385789, 0.003984994205949118, 0.011428678456896373, 0.0027024152988841896, 0.042005499725970064, -0.014099307479139123, 0.0021737037873166033, -0.015045743623915806  …  0.02598928757621858, 0.023974826709166574, -0.005025111950753263, -0.016594864696717528, 0.008055002833526812, -0.01638615916647568, 0.02887626205712443, 0.003494536135952917, 0.011750890910482606, 0.03564100073253037]               "
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "iter"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We incorporate a convergence test in the function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "cg_quadratic_tol (generic function with 3 methods)"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "function cg_quadratic_tol(A:: Matrix, b:: Vector, x0:: Vector, trace:: Bool = false, tol = 1e-8)\n",
    "    n = length(x0)\n",
    "    x = x0\n",
    "    if (trace)\n",
    "        iter = [ x ]\n",
    "    end\n",
    "    g = b+A*x\n",
    "    d = -g\n",
    "    k = 0\n",
    "    \n",
    "    tol2 = tol*tol\n",
    "\n",
    "    β = 0.0\n",
    "\n",
    "    while ((dot(g,g) > tol2) && (k <= n))\n",
    "        Ad = A*d\n",
    "        normd = dot(d,Ad)\n",
    "        α = dot(g,g)/normd\n",
    "#        α = -dot(d,g)/normd\n",
    "        x += α*d\n",
    "        if (trace)\n",
    "            iter = [ iter; x ]\n",
    "        end\n",
    "        g = b+A*x\n",
    "        β = dot(g,Ad)/normd\n",
    "        d = -g+β*d\n",
    "        k += 1\n",
    "    end\n",
    "\n",
    "    if (trace)\n",
    "        iter = [ iter; x ]\n",
    "        return x, iter, k\n",
    "    end\n",
    "\n",
    "    return x, k\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([0.01597451882112203, 0.028709623791649267, -0.03192775433157097, 0.003984994201861061, 0.0114286784498273, 0.002702415282237887, 0.042005499733811784, -0.014099307476687226, 0.0021737037789500433, -0.015045743622889955  …  0.02598928757205917, 0.02397482672012732, -0.005025111963837796, -0.016594864698436115, 0.008055002822780828, -0.01638615914812406, 0.028876262058443115, 0.0034945361401307992, 0.011750890910753055, 0.03564100073400417], Any[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 0.0002767606413135444, 0.0002733315138966624, -0.0010957482140589786, 0.00342103697035908, 0.0018584951036187565, 0.0017303923802678986, 0.0035685063965333794, 0.0015940332391309, 0.0016510508412575953  …  0.02598928757205917, 0.02397482672012732, -0.005025111963837796, -0.016594864698436115, 0.008055002822780828, -0.01638615914812406, 0.028876262058443115, 0.0034945361401307992, 0.011750890910753055, 0.03564100073400417], 193)"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x, iter, k = cg_quadratic_tol(A, b, x0, true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The number of iterations is"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "193"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "k"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Are we close to the solution?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.2296899834490661e-10"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "norm(b1-x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(500, 500)"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "size(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "which is much less than the problem dimension"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Preconditioned conjugate gradient"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A basic implementation of a preconditioned conjugate gradient algorithm follows, were $M$ is the inverse of the preconditioner to apply."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pcg_quadratic_tol (generic function with 3 methods)"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "function pcg_quadratic_tol(A:: Matrix, b:: Vector, x0:: Vector, M:: Matrix,\n",
    "                           trace:: Bool = false, tol = 1e-8)\n",
    "    n = length(x0)\n",
    "    x = x0\n",
    "    if (trace)\n",
    "        iter = [ x ]\n",
    "    end\n",
    "    g = b+A*x\n",
    "    v = M*g\n",
    "    d = -v\n",
    "    k = 0\n",
    "    \n",
    "    tol2 = tol*tol\n",
    "\n",
    "    β = 0.0\n",
    "\n",
    "    gv = dot(g,v)\n",
    "    while ((gv > tol2) && (k <= n))\n",
    "#    while ((dot(g,g) > tol2) && (k <= n))\n",
    "        Ad = A*d\n",
    "        normd = dot(d,Ad)\n",
    "        #gv = dot(g,v)\n",
    "        α = gv/normd\n",
    "        x += α*d\n",
    "        if (trace)\n",
    "            iter = [ iter; x ]\n",
    "        end\n",
    "        g += α*Ad\n",
    "        v = M*g\n",
    "        gvold = gv\n",
    "        gv = dot(g,v)\n",
    "        β = gv/gvold\n",
    "        d = -v+β*d\n",
    "        k += 1\n",
    "    end\n",
    "\n",
    "    if (trace)\n",
    "        iter = [ iter; x ]\n",
    "        return x, iter, k\n",
    "    end\n",
    "\n",
    "    return x, k\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's check first that when there is no preconditioning, we obtain the same iterates.\n",
    "Set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([0.015974518821207862, 0.028709623792143396, -0.031927754330216294, 0.003984994202316493, 0.011428678450270907, 0.0027024152828897534, 0.04200549973349374, -0.014099307476557727, 0.0021737037797074257, -0.015045743623517504  …  0.025989287572569776, 0.02397482672008192, -0.005025111963021935, -0.01659486469889695, 0.008055002822959107, -0.01638615914854714, 0.02887626205801894, 0.0034945361396659463, 0.011750890910751113, 0.03564100073428025], Any[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], 0.0002767606413135444, 0.0002733315138966624, -0.0010957482140589786, 0.00342103697035908, 0.0018584951036187565, 0.0017303923802678986, 0.0035685063965333794, 0.0015940332391309, 0.0016510508412575953  …  0.025989287572569776, 0.02397482672008192, -0.005025111963021935, -0.01659486469889695, 0.008055002822959107, -0.01638615914854714, 0.02887626205801894, 0.0034945361396659463, 0.011750890910751113, 0.03564100073428025], 191)"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "M = zeros(n,n)+I\n",
    "x, iter, k = pcg_quadratic_tol(A, b, x0, M, true)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(191, 2.1840922688602002e-10)"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "k, norm(x-b1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can compute the eigenvalues and condition number of $A$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Eigen{Float64,Float64,Array{Float64,2},Array{Float64,1}}\n",
       "eigenvalues:\n",
       "500-element Array{Float64,1}:\n",
       "    5.6347873665070125\n",
       "    6.568444017779966 \n",
       "    7.489746666278461 \n",
       "    7.844986364490883 \n",
       "    8.161787767359893 \n",
       "    8.694467252631284 \n",
       "    9.013297019850604 \n",
       "   10.122452124852767 \n",
       "   10.447788330344956 \n",
       "   11.64997528781129  \n",
       "   12.091908992319638 \n",
       "   13.294468830274475 \n",
       "   13.420305154608918 \n",
       "    ⋮                 \n",
       " 1920.9222115923171   \n",
       " 1946.2393789278747   \n",
       " 1954.906424415574    \n",
       " 1990.7946362198902   \n",
       " 2006.3267494852591   \n",
       " 2019.6958115404982   \n",
       " 2023.013770835366    \n",
       " 2056.7645567980553   \n",
       " 2070.654706568727    \n",
       " 2119.4968773451974   \n",
       " 2160.4366473431965   \n",
       " 2208.6742110658283   \n",
       "eigenvectors:\n",
       "500×500 Array{Float64,2}:\n",
       " -0.0108411    0.0830059    0.0337321   …   0.00544394   -0.0368143  \n",
       " -0.0166809    0.037664     0.00301322     -0.0647637     0.00924254 \n",
       " -0.0109104   -0.0298255    0.0202361      -0.0166545     0.0450626  \n",
       " -0.0260085   -0.0357509    0.0126751       0.0418049    -0.00330673 \n",
       "  0.0322644   -0.00148673   0.00337096      0.0385246     0.00975553 \n",
       "  0.0380427   -0.0598454   -0.0041391   …   0.0414176     0.0200765  \n",
       "  0.0540155   -0.0218164    0.0112326       0.000309059  -0.0145069  \n",
       "  0.0490615   -0.0545569   -0.0604463      -0.00714152    0.0295854  \n",
       " -0.016645     0.029212     0.0146597       0.0300499    -0.0768382  \n",
       " -0.0263591    0.0414314   -0.0366439      -0.0521502     0.000549767\n",
       "  0.0644336   -0.0018098   -0.0198142   …   0.028083     -0.00764548 \n",
       " -0.00733239  -0.0345237   -0.00739582      0.0120333     0.0197079  \n",
       "  0.00716474   0.0595677   -0.00684312     -0.0251253     0.0884429  \n",
       "  ⋮                                     ⋱                            \n",
       "  0.0593369    0.0157047   -0.0327297      -0.0161573    -0.0467215  \n",
       "  0.0409041    0.020197     0.0343764       0.0536806     0.026552   \n",
       "  0.015537     0.0278595   -0.00738675  …   0.0203819    -0.0692414  \n",
       "  0.0185884   -0.0933783   -0.0628894       0.00586828   -0.0618242  \n",
       " -0.0677665   -0.0291168   -0.0213424      -0.0376346    -0.0146434  \n",
       "  0.00682537  -0.0861847   -0.0200911       0.00922648    0.0300396  \n",
       "  0.0360382    0.0394547    0.025104        0.0285377     0.0535781  \n",
       "  0.00720599   0.0565453   -0.117067    …  -0.0126409     0.00674555 \n",
       " -0.00305482   0.0219819   -0.00383059     -0.000606488   0.0137448  \n",
       " -0.0331448   -0.0832012    0.0544898       0.0074725     0.0331266  \n",
       " -0.0314098   -0.0269488    0.00602694      0.0579473    -0.00717918 \n",
       "  0.0861724    0.0824555    0.0183425       0.00455581    0.0199025  "
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eigen(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "391.9711725404463"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cond(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Try to compute a simple precontionner using the inverse of the diagonal of matrix $A$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "500×500 Diagonal{Float64,Array{Float64,1}}:\n",
       " 0.00180538   ⋅           ⋅          …   ⋅           ⋅           ⋅        \n",
       "  ⋅          0.00162809   ⋅              ⋅           ⋅           ⋅        \n",
       "  ⋅           ⋅          0.00166734      ⋅           ⋅           ⋅        \n",
       "  ⋅           ⋅           ⋅              ⋅           ⋅           ⋅        \n",
       "  ⋅           ⋅           ⋅              ⋅           ⋅           ⋅        \n",
       "  ⋅           ⋅           ⋅          …   ⋅           ⋅           ⋅        \n",
       "  ⋅           ⋅           ⋅              ⋅           ⋅           ⋅        \n",
       "  ⋅           ⋅           ⋅              ⋅           ⋅           ⋅        \n",
       "  ⋅           ⋅           ⋅              ⋅           ⋅           ⋅        \n",
       "  ⋅           ⋅           ⋅              ⋅           ⋅           ⋅        \n",
       "  ⋅           ⋅           ⋅          …   ⋅           ⋅           ⋅        \n",
       "  ⋅           ⋅           ⋅              ⋅           ⋅           ⋅        \n",
       "  ⋅           ⋅           ⋅              ⋅           ⋅           ⋅        \n",
       " ⋮                                   ⋱                                    \n",
       "  ⋅           ⋅           ⋅              ⋅           ⋅           ⋅        \n",
       "  ⋅           ⋅           ⋅              ⋅           ⋅           ⋅        \n",
       "  ⋅           ⋅           ⋅          …   ⋅           ⋅           ⋅        \n",
       "  ⋅           ⋅           ⋅              ⋅           ⋅           ⋅        \n",
       "  ⋅           ⋅           ⋅              ⋅           ⋅           ⋅        \n",
       "  ⋅           ⋅           ⋅              ⋅           ⋅           ⋅        \n",
       "  ⋅           ⋅           ⋅              ⋅           ⋅           ⋅        \n",
       "  ⋅           ⋅           ⋅          …   ⋅           ⋅           ⋅        \n",
       "  ⋅           ⋅           ⋅              ⋅           ⋅           ⋅        \n",
       "  ⋅           ⋅           ⋅             0.00168698   ⋅           ⋅        \n",
       "  ⋅           ⋅           ⋅              ⋅          0.00161359   ⋅        \n",
       "  ⋅           ⋅           ⋅              ⋅           ⋅          0.00162828"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "D = 1 ./diag(A)\n",
    "M = Diagonal(D)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Unfortunately, in this case, it does not help as the condition number is not improving."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "392.7861780853309"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B = M*A\n",
    "cond(B)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Consider another situation when $A$ is diagonal."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "573.7929667509559"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = 1000;\n",
    "A = zeros(n,n);\n",
    "for i = 1:n\n",
    "    A[i,i] = 10*rand()\n",
    "end\n",
    "b = zeros(n)\n",
    "for i = 1:n\n",
    "  b[i] = rand()\n",
    "end\n",
    "x0 = zeros(n)\n",
    "cond(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The solution we are looking for is"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1000-element Array{Float64,1}:\n",
       " 0.08511153833584434  \n",
       " 0.0006468123709911861\n",
       " 1.0013983310364591   \n",
       " 0.16609043417698013  \n",
       " 0.06556649716468967  \n",
       " 0.053305142501921275 \n",
       " 0.1330303200775495   \n",
       " 0.07521223614895942  \n",
       " 0.07434309714215172  \n",
       " 0.11919717174421243  \n",
       " 0.3395450294971931   \n",
       " 0.04731761256378243  \n",
       " 0.12257493102570208  \n",
       " ⋮                    \n",
       " 0.13967384586652654  \n",
       " 0.1994659576124026   \n",
       " 0.20576921142916305  \n",
       " 0.03653041453119203  \n",
       " 0.12633530344223995  \n",
       " 0.16457566391926592  \n",
       " 0.1860308402751664   \n",
       " 0.23193104246255242  \n",
       " 0.14603650854633932  \n",
       " 0.09020585221888404  \n",
       " 0.04861227773813524  \n",
       " 0.031573428647867274 "
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A\\b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Without preconditionning, with have the iterates sequence"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([-0.0851115383358446, -0.0006468123709942869, -1.0013983311042045, -0.16609043419701267, -0.06556649715409481, -0.05330514259925526, -0.13303032010196264, -0.07521223610378068, -0.07434309714948052, -0.11919717177496945  …  -0.2057692115018965, -0.036530414509147145, -0.12633530342222551, -0.1645756638529031, -0.18603084046921495, -0.2319310422065388, -0.1460365084758426, -0.09020585223805899, -0.048612277779012024, -0.031573428667768966], Any[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], -0.16771570209428255, -0.0012785774778700473, -0.17187852852030042, -0.17944655439538684, -0.11900440761468015, -0.10204472832347751, -0.1746280084912797, -0.057858758881205334, -0.08306066632162536  …  -0.2057692115018965, -0.036530414509147145, -0.12633530342222551, -0.1645756638529031, -0.18603084046921495, -0.2319310422065388, -0.1460365084758426, -0.09020585223805899, -0.048612277779012024, -0.031573428667768966], 182)"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "M = zeros(n,n)+I\n",
    "x, iter, k = pcg_quadratic_tol(A, b, x0, M, true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is equivalent to the unpreconditioned version."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([-0.08511153833584394, -0.000646812370994591, -1.0013983311042047, -0.16609043419701278, -0.06556649715409484, -0.05330514259925528, -0.13303032010196272, -0.07521223610378056, -0.0743430971494805, -0.11919717177496944  …  -0.20576921150189653, -0.03653041450914715, -0.12633530342222543, -0.16457566385290312, -0.18603084046921484, -0.23193104220653885, -0.1460365084758425, -0.09020585223805905, -0.04861227777901204, -0.031573428667768924], Any[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], -0.16771570209428255, -0.0012785774778700473, -0.17187852852030042, -0.17944655439538684, -0.11900440761468015, -0.10204472832347751, -0.1746280084912797, -0.057858758881205334, -0.08306066632162536  …  -0.20576921150189653, -0.03653041450914715, -0.12633530342222543, -0.16457566385290312, -0.18603084046921484, -0.23193104220653885, -0.1460365084758425, -0.09020585223805905, -0.04861227777901204, -0.031573428667768924], 182)"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x, iter, k = cg_quadratic_tol(A, b, x0, true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "However, since $A$ is diagonal, an obvious diagonal preconditionner is $A^{-1}$ itself."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "M = zeros(n,n)\n",
    "for i = 1:n\n",
    "    M[i,i] = 1/A[i,i]\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The condition number of the preconditioned matrix is of course equal to 1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0000000000000002"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cond(M*A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The theory then predicts that we converge in one iteration with the precionditionned conjugate gradient."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([-0.08511153833584435, -0.0006468123709911862, -1.0013983310364591, -0.1660904341769802, -0.06556649716468968, -0.053305142501921296, -0.1330303200775495, -0.07521223614895943, -0.07434309714215173, -0.11919717174421246  …  -0.20576921142916313, -0.03653041453119204, -0.12633530344223998, -0.16457566391926595, -0.18603084027516642, -0.23193104246255247, -0.14603650854633934, -0.09020585221888405, -0.04861227773813525, -0.03157342864786728], Any[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], -0.08511153833584435, -0.0006468123709911862, -1.0013983310364591, -0.1660904341769802, -0.06556649716468968, -0.053305142501921296, -0.1330303200775495, -0.07521223614895943, -0.07434309714215173  …  -0.20576921142916313, -0.03653041453119204, -0.12633530344223998, -0.16457566391926595, -0.18603084027516642, -0.23193104246255247, -0.14603650854633934, -0.09020585221888405, -0.04861227773813525, -0.03157342864786728], 1)"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x, iter, k = pcg_quadratic_tol(A, b, x0, M, true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Consider now another example."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1000×1000 Array{Float64,2}:\n",
       " 3.0  1.4  0.0  0.0  0.0  0.0  0.0  0.0  …  0.0  0.0  0.0  0.0  0.0  0.0  0.0\n",
       " 1.4  3.0  1.4  0.0  0.0  0.0  0.0  0.0     0.0  0.0  0.0  0.0  0.0  0.0  0.0\n",
       " 0.0  1.4  3.0  1.4  0.0  0.0  0.0  0.0     0.0  0.0  0.0  0.0  0.0  0.0  0.0\n",
       " 0.0  0.0  1.4  3.0  1.4  0.0  0.0  0.0     0.0  0.0  0.0  0.0  0.0  0.0  0.0\n",
       " 0.0  0.0  0.0  1.4  3.0  1.4  0.0  0.0     0.0  0.0  0.0  0.0  0.0  0.0  0.0\n",
       " 0.0  0.0  0.0  0.0  1.4  3.0  1.4  0.0  …  0.0  0.0  0.0  0.0  0.0  0.0  0.0\n",
       " 0.0  0.0  0.0  0.0  0.0  1.4  3.0  1.4     0.0  0.0  0.0  0.0  0.0  0.0  0.0\n",
       " 0.0  0.0  0.0  0.0  0.0  0.0  1.4  3.0     0.0  0.0  0.0  0.0  0.0  0.0  0.0\n",
       " 0.0  0.0  0.0  0.0  0.0  0.0  0.0  1.4     0.0  0.0  0.0  0.0  0.0  0.0  0.0\n",
       " 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0     0.0  0.0  0.0  0.0  0.0  0.0  0.0\n",
       " 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  …  0.0  0.0  0.0  0.0  0.0  0.0  0.0\n",
       " 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0     0.0  0.0  0.0  0.0  0.0  0.0  0.0\n",
       " 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0     0.0  0.0  0.0  0.0  0.0  0.0  0.0\n",
       " ⋮                        ⋮              ⋱            ⋮                      \n",
       " 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0     0.0  0.0  0.0  0.0  0.0  0.0  0.0\n",
       " 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0     0.0  0.0  0.0  0.0  0.0  0.0  0.0\n",
       " 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  …  0.0  0.0  0.0  0.0  0.0  0.0  0.0\n",
       " 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0     0.0  0.0  0.0  0.0  0.0  0.0  0.0\n",
       " 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0     1.4  0.0  0.0  0.0  0.0  0.0  0.0\n",
       " 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0     3.0  1.4  0.0  0.0  0.0  0.0  0.0\n",
       " 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0     1.4  3.0  1.4  0.0  0.0  0.0  0.0\n",
       " 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  …  0.0  1.4  3.0  1.4  0.0  0.0  0.0\n",
       " 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0     0.0  0.0  1.4  3.0  1.4  0.0  0.0\n",
       " 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0     0.0  0.0  0.0  1.4  3.0  1.4  0.0\n",
       " 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0     0.0  0.0  0.0  0.0  1.4  3.0  1.4\n",
       " 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0     0.0  0.0  0.0  0.0  0.0  1.4  3.0"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = zeros(n,n)+3*I\n",
    "for i = 1:n-1\n",
    "    A[i,i+1] = 1.4\n",
    "    A[i+1,i] = 1.4\n",
    "end\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Eigen{Float64,Float64,Array{Float64,2},Array{Float64,1}}\n",
       "eigenvalues:\n",
       "1000-element Array{Float64,1}:\n",
       " 0.20001378984134793\n",
       " 0.20005515922956107\n",
       " 0.20012410775715758\n",
       " 0.20022063474500001\n",
       " 0.20034473924231072\n",
       " 0.2004964200266737 \n",
       " 0.2006756756040492 \n",
       " 0.20088250420879217\n",
       " 0.20111690380366315\n",
       " 0.20137887207985267\n",
       " 0.20166840645700262\n",
       " 0.20198550408323332\n",
       " 0.20233016183516794\n",
       " ⋮                  \n",
       " 5.7980144959167665 \n",
       " 5.798331593542997  \n",
       " 5.798621127920147  \n",
       " 5.798883096196337  \n",
       " 5.799117495791208  \n",
       " 5.7993243243959505 \n",
       " 5.799503579973327  \n",
       " 5.79965526075769   \n",
       " 5.799779365255     \n",
       " 5.799875892242843  \n",
       " 5.799944840770439  \n",
       " 5.799986210158653  \n",
       "eigenvectors:\n",
       "1000×1000 Array{Float64,2}:\n",
       " -0.000140286  -0.00028057   -0.000420851  …   0.00028057   0.000140286\n",
       "  0.00028057    0.000561129   0.000841665      0.000561129  0.00028057 \n",
       " -0.000420851  -0.000841665  -0.0012624        0.000841665  0.000420851\n",
       "  0.000561129   0.00112217    0.00168303       0.00112217   0.000561129\n",
       " -0.0007014    -0.00140263   -0.00210351       0.00140263   0.0007014  \n",
       "  0.000841665   0.00168303    0.0025238    …   0.00168303   0.000841665\n",
       " -0.000981922  -0.00196337   -0.00294387       0.00196337   0.000981922\n",
       "  0.00112217    0.00224363    0.00336368       0.00224363   0.00112217 \n",
       " -0.0012624    -0.0025238    -0.00378319       0.0025238    0.0012624  \n",
       "  0.00140263    0.00280387    0.00420236       0.00280387   0.00140263 \n",
       " -0.00154284   -0.00308384   -0.00462116   …   0.00308384   0.00154284 \n",
       "  0.00168303    0.00336368    0.00503955       0.00336368   0.00168303 \n",
       " -0.00182321   -0.00364338   -0.0054575        0.00364338   0.00182321 \n",
       "  ⋮                                        ⋱                           \n",
       " -0.00168303    0.00336368   -0.00503955      -0.00336368   0.00168303 \n",
       "  0.00154284   -0.00308384    0.00462116      -0.00308384   0.00154284 \n",
       " -0.00140263    0.00280387   -0.00420236   …  -0.00280387   0.00140263 \n",
       "  0.0012624    -0.0025238     0.00378319      -0.0025238    0.0012624  \n",
       " -0.00112217    0.00224363   -0.00336368      -0.00224363   0.00112217 \n",
       "  0.000981922  -0.00196337    0.00294387      -0.00196337   0.000981922\n",
       " -0.000841665   0.00168303   -0.0025238       -0.00168303   0.000841665\n",
       "  0.0007014    -0.00140263    0.00210351   …  -0.00140263   0.0007014  \n",
       " -0.000561129   0.00112217   -0.00168303      -0.00112217   0.000561129\n",
       "  0.000420851  -0.000841665   0.0012624       -0.000841665  0.000420851\n",
       " -0.00028057    0.000561129  -0.000841665     -0.000561129  0.00028057 \n",
       "  0.000140286  -0.00028057    0.000420851     -0.00028057   0.000140286"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eigen(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1000-element Array{Float64,1}:\n",
       " -0.5302289407584934 \n",
       "  0.5320572116235507 \n",
       " -0.614499363830206  \n",
       "  0.16558409106245517\n",
       " -0.3867283770710757 \n",
       "  0.23444036479376498\n",
       " -0.48323066739804693\n",
       "  0.17200666195791722\n",
       " -0.09377460297319108\n",
       " -0.2702632857913654 \n",
       "  0.07743925607339158\n",
       " -0.24311535606791693\n",
       "  0.11472168208891691\n",
       "  ⋮                  \n",
       "  0.33685798270574924\n",
       " -0.41825398526282176\n",
       " -0.11951583027789782\n",
       "  0.20944855966974235\n",
       " -0.4354505818938196 \n",
       "  0.19390325946301898\n",
       " -0.37979197192850783\n",
       "  0.3177259668162186 \n",
       " -0.5326302518132492 \n",
       "  0.12218995452625997\n",
       " -0.15109683644442018\n",
       " -0.02980410623663226"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A\\(-b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([-0.5302289403705569, 0.5320572110460983, -0.6144993633198529, 0.16558409070301935, -0.3867283766906912, 0.2344403645353679, -0.48323066718196606, 0.17200666191450553, -0.09377460293883143, -0.2702632857715988  …  -0.11951582966392606, 0.2094485592538897, -0.43545058159381483, 0.19390325943627734, -0.3797919719216848, 0.31772596691149946, -0.5326302519660845, 0.12218995462017966, -0.15109683666282, -0.029804106119259707], Any[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], -0.16529460248077343, -0.001260120270829454, -0.16939733544311864, -0.17685611129395332, -0.11728649139285964, -0.10057163755606846, -0.17210712464685335, -0.05702352510752649, -0.08186162446318297  …  -0.11951582966392606, 0.2094485592538897, -0.43545058159381483, 0.19390325943627734, -0.3797919719216848, 0.31772596691149946, -0.5326302519660845, 0.12218995462017966, -0.15109683666282, -0.029804106119259707], 56)"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x, iter, k = cg_quadratic_tol(A, b, x0, true)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [],
   "source": [
    "M = zeros(n,n)\n",
    "for i = 1:n\n",
    "    M[i,i] = 1/A[i,i]\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "28.997931666407833"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cond(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "28.997931666407865"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cond(M*A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([-0.5302289402041654, 0.5320572109436659, -0.6144993631382352, 0.1655840901811337, -0.386728376366776, 0.234440364226002, -0.4832306671692573, 0.1720066621540863, -0.09377460338549971, -0.2702632856365679  …  -0.11951582981798614, 0.209448559167586, -0.43545058168730405, 0.19390325937609734, -0.3797919723525358, 0.3177259673524759, -0.5326302520995585, 0.12218995472152971, -0.15109683643578578, -0.029804106122671817], Any[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], -0.16529460248077343, -0.0012601202708294541, -0.16939733544311866, -0.17685611129395334, -0.11728649139285965, -0.10057163755606847, -0.17210712464685338, -0.05702352510752649, -0.08186162446318297  …  -0.11951582981798614, 0.209448559167586, -0.43545058168730405, 0.19390325937609734, -0.3797919723525358, 0.3177259673524759, -0.5326302520995585, 0.12218995472152971, -0.15109683643578578, -0.029804106122671817], 55)"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x, iter, k = pcg_quadratic_tol(A, b, x0, M, true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There is no advantage."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1000×1000 Array{Float64,2}:\n",
       "  0.490553      -0.336899       0.231373      …   5.26731e-164  -2.45808e-164\n",
       " -0.336899       0.721926      -0.4958           -1.12871e-163   5.26731e-164\n",
       "  0.231373      -0.4958         0.831055          1.89193e-163  -8.82901e-164\n",
       " -0.158901       0.340503      -0.570747         -2.92543e-163   1.3652e-163 \n",
       "  0.109129      -0.233848       0.391974          4.37685e-163  -2.04253e-163\n",
       " -0.0749471      0.160601      -0.269198      …  -6.45353e-163   3.01165e-163\n",
       "  0.0514717     -0.110297       0.184878          9.45214e-163  -4.411e-163  \n",
       " -0.0353494      0.0757488     -0.126969         -1.38011e-162   6.44049e-163\n",
       "  0.0242771     -0.0520223      0.0871993         2.01216e-162  -9.39006e-163\n",
       " -0.0166729      0.0357276     -0.0598862        -2.93166e-162   1.36811e-162\n",
       "  0.0114505     -0.0245368      0.0411283     …   4.26996e-162  -1.99265e-162\n",
       " -0.00786389     0.0168512     -0.0282458        -6.21827e-162   2.90186e-162\n",
       "  0.00540072    -0.011573       0.0193985         9.05489e-162  -4.22562e-162\n",
       "  ⋮                                           ⋱                              \n",
       "  2.90186e-162  -6.21827e-162   1.0423e-161       0.0168512     -0.00786389  \n",
       " -1.99265e-162   4.26996e-162  -7.15727e-162     -0.0245368      0.0114505   \n",
       "  1.36811e-162  -2.93166e-162   4.91401e-162  …   0.0357276     -0.0166729   \n",
       " -9.39006e-163   2.01216e-162  -3.37276e-162     -0.0520223      0.0242771   \n",
       "  6.44049e-163  -1.38011e-162   2.31332e-162      0.0757488     -0.0353494   \n",
       " -4.411e-163     9.45214e-163  -1.58436e-162     -0.110297       0.0514717   \n",
       "  3.01165e-163  -6.45353e-163   1.08173e-162      0.160601      -0.0749471   \n",
       " -2.04253e-163   4.37685e-163  -7.33643e-163  …  -0.233848       0.109129    \n",
       "  1.3652e-163   -2.92543e-163   4.90358e-163      0.340503      -0.158901    \n",
       " -8.82901e-164   1.89193e-163  -3.17124e-163     -0.4958         0.231373    \n",
       "  5.26731e-164  -1.12871e-163   1.89193e-163      0.721926      -0.336899    \n",
       " -2.45808e-164   5.26731e-164  -8.82901e-164     -0.336899       0.490553    "
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "M = A^(-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([-0.5302289407584935, 0.5320572116235506, -0.6144993638302059, 0.16558409106245536, -0.386728377071076, 0.23444036479376512, -0.48323066739804726, 0.17200666195791728, -0.09377460297319118, -0.27026328579136544  …  -0.1195158302778978, 0.20944855966974238, -0.4354505818938197, 0.19390325946301892, -0.379791971928508, 0.31772596681621873, -0.5326302518132492, 0.12218995452626004, -0.1510968364444202, -0.029804106236632273], Any[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], -0.5302289407584935, 0.5320572116235506, -0.6144993638302059, 0.16558409106245536, -0.386728377071076, 0.23444036479376512, -0.48323066739804726, 0.17200666195791728, -0.09377460297319118  …  -0.1195158302778978, 0.20944855966974238, -0.4354505818938197, 0.19390325946301892, -0.379791971928508, 0.31772596681621873, -0.5326302518132492, 0.12218995452626004, -0.1510968364444202, -0.029804106236632273], 1)"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x, iter, k = pcg_quadratic_tol(A, b, x0, M, true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Consider now the following example."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1000×1000 Array{Float64,2}:\n",
       " 3.0  0.0   0.0   0.0   0.0   0.0  …       0.0       0.0       0.0  0.0  \n",
       " 0.0  6.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0  11.0   0.0   0.0   0.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0  18.0   0.0   0.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0  27.0   0.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0  38.0  …       0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0  …       0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  0.0  \n",
       " ⋮                            ⋮    ⋱                                     \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0  …       0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0  …       0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0     994011.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0          0.0  996006.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0          0.0       0.0  998003.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  1.0e6"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = 1000\n",
    "A = zeros(n,n)+Diagonal([2+i*i for i=1:n])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "372201.88311699365"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for i = 1:n-1\n",
    "    A[i,i+1] = 1\n",
    "    A[i+1,i] = 1\n",
    "end\n",
    "A[n,1] = 1\n",
    "A[1,n] = 1\n",
    "cond(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1000×1000 Array{Float64,2}:\n",
       " 3.0  1.0   0.0   0.0   0.0   0.0  …       0.0       0.0       0.0  1.0  \n",
       " 1.0  6.0   1.0   0.0   0.0   0.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  1.0  11.0   1.0   0.0   0.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   1.0  18.0   1.0   0.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   1.0  27.0   1.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   1.0  38.0  …       0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   1.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0  …       0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  0.0  \n",
       " ⋮                            ⋮    ⋱                                     \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0  …       0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0  …       1.0       0.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0     994011.0       1.0       0.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0          1.0  996006.0       1.0  0.0  \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0          0.0       1.0  998003.0  1.0  \n",
       " 1.0  0.0   0.0   0.0   0.0   0.0          0.0       0.0       1.0  1.0e6"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9967271248797949"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "κ = cond(A)\n",
    "(sqrt(κ)-1)/(sqrt(κ)+1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1000×1000 Array{Float64,2}:\n",
       "  0.353263     -0.0597876     0.00546288   …   3.53969e-13  -3.53262e-7 \n",
       " -0.0597876     0.179363     -0.0163886       -5.99071e-14   5.97875e-8 \n",
       "  0.00546288   -0.0163886     0.092869         5.4738e-15   -5.46287e-9 \n",
       " -0.00030412    0.000912359  -0.00517004      -3.04728e-16   3.04119e-10\n",
       "  1.12747e-5   -3.38241e-5    0.00019167       1.12972e-17  -1.12747e-11\n",
       " -2.96856e-7    8.90567e-7   -5.04654e-6   …  -2.97449e-19   2.96855e-13\n",
       "  5.82243e-9   -1.74673e-8    9.89813e-8       5.83407e-21  -5.82242e-15\n",
       " -8.82347e-11   2.64704e-10  -1.49999e-9      -8.84111e-23   8.82346e-17\n",
       "  1.06319e-12  -3.18958e-12   1.80743e-11      1.06532e-24  -1.06319e-18\n",
       " -1.04243e-14   3.12729e-14  -1.77213e-13     -1.04451e-26   1.04243e-20\n",
       "  8.47552e-17  -2.54265e-16   1.44084e-15  …   8.49246e-29  -8.4755e-23 \n",
       " -5.80538e-19   1.74161e-18  -9.86915e-18     -5.81699e-31   5.80537e-25\n",
       "  3.39506e-21  -1.01852e-20   5.7716e-20       3.40185e-33  -3.39505e-27\n",
       "  ⋮                                        ⋱                            \n",
       "  4.03306e-73  -6.82572e-74   6.23676e-75      1.14166e-66  -1.14166e-72\n",
       " -3.94483e-67   6.67639e-68  -6.10032e-69     -1.11669e-60   1.11669e-66\n",
       "  3.86634e-61  -6.54355e-62   5.97894e-63  …   1.09447e-54  -1.09447e-60\n",
       " -3.79706e-55   6.4263e-56   -5.87181e-57     -1.07486e-48   1.07486e-54\n",
       "  3.73656e-49  -6.32391e-50   5.77825e-51      1.05773e-42  -1.05773e-48\n",
       " -3.68444e-43   6.2357e-44   -5.69765e-45     -1.04298e-36   1.04298e-42\n",
       "  3.64037e-37  -6.1611e-38    5.62949e-39      1.0305e-30   -1.0305e-36 \n",
       " -3.60406e-31   6.09966e-32  -5.57335e-33  …  -1.02022e-24   1.02022e-30\n",
       "  3.57529e-25  -6.05097e-26   5.52887e-27      1.01208e-18  -1.01208e-24\n",
       " -3.55388e-19   6.01473e-20  -5.49575e-21     -1.00602e-12   1.00602e-18\n",
       "  3.53969e-13  -5.99071e-14   5.4738e-15       1.002e-6     -1.002e-12  \n",
       " -3.53262e-7    5.97875e-8   -5.46287e-9      -1.002e-12     9.99998e-7 "
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A^(-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([-0.3033119021203991, 0.05994014657633306, -0.08001978468687258, -0.04422351949800128, -0.02095934787711228, -0.011914718169687078, -0.017020464511779073, -0.004390717558910035, -0.004772806836022754, -0.00802569923839308  …  -5.223710448619647e-7, -1.5112923825799127e-7, -7.521537523920856e-7, -5.667000138195912e-7, -4.6473990358393947e-7, -3.266710880132152e-7, -9.8792630635204e-7, -5.929959676495789e-7, -3.2468772915646726e-7, -1.7702498323373305e-7], Any[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], -2.4915369033636528e-6, -1.8994184385502717e-8, -2.5533786721021e-6, -2.665807118084986e-6, -1.767895727904024e-6, -1.5159475423996675e-6, -2.5942241667529414e-6, -8.595333122375362e-7, -1.2339257014943442e-6  …  -5.223710448619647e-7, -1.5112923825799127e-7, -7.521537523920856e-7, -5.667000138195912e-7, -4.6473990358393947e-7, -3.266710880132152e-7, -9.8792630635204e-7, -5.929959676495789e-7, -3.2468772915646726e-7, -1.7702498323373305e-7], 1001)"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "M = zeros(n,n)+I\n",
    "x, iter, k = pcg_quadratic_tol(A, b, x0, M, true)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1.926360732450869, 372201.88311699365)"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "M = zeros(n,n)\n",
    "for i = 1:n\n",
    "    M[i,i] = 1/A[i,i]\n",
    "end\n",
    "cond(A*M), cond(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1000×1000 Array{Float64,2}:\n",
       " 0.333333  0.0       0.0        …  0.0         0.0       0.0       \n",
       " 0.0       0.166667  0.0           0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0909091     0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0           0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0           0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0        …  0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0           0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0           0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0           0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0           0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0        …  0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0           0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0           0.0         0.0       0.0       \n",
       " ⋮                              ⋱                                  \n",
       " 0.0       0.0       0.0           0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0           0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0        …  0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0           0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0           0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0           0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0           0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0        …  0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0           0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0           1.00401e-6  0.0       0.0       \n",
       " 0.0       0.0       0.0           0.0         1.002e-6  0.0       \n",
       " 0.0       0.0       0.0           0.0         0.0       9.99998e-7"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "M"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1000×1000 Array{Float64,2}:\n",
       " 1.0       0.166667  0.0        …  0.0         0.0       9.99998e-7\n",
       " 0.333333  1.0       0.0909091     0.0         0.0       0.0       \n",
       " 0.0       0.166667  1.0           0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0909091     0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0           0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0        …  0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0           0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0           0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0           0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0           0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0        …  0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0           0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0           0.0         0.0       0.0       \n",
       " ⋮                              ⋱                                  \n",
       " 0.0       0.0       0.0           0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0           0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0        …  0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0           0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0           0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0           0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0           0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0        …  0.0         0.0       0.0       \n",
       " 0.0       0.0       0.0           1.00401e-6  0.0       0.0       \n",
       " 0.0       0.0       0.0           1.0         1.002e-6  0.0       \n",
       " 0.0       0.0       0.0           1.00401e-6  1.0       9.99998e-7\n",
       " 0.333333  0.0       0.0           0.0         1.002e-6  1.0       "
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A*M"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([-0.30287293549210814, 0.06281207850145543, -0.08044752709136636, -0.04468954806074069, -0.02010717159693447, -0.012567712035396262, -0.016941340441385185, -0.004090090650218402, -0.004900069594150625, -0.008087212138416136  …  -6.627484313750857e-7, -1.5101211052488287e-7, -7.52150227113502e-7, -5.664039430184978e-7, -4.2735612965806707e-7, -3.2682047950118643e-7, -9.879242267200632e-7, -5.930152520612125e-7, -3.2459795367496876e-7, 1.9253629660874997e-9], Any[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], -0.27402478474876785, -0.0010445113778345088, -0.07658898555859281, -0.04886523526427786, -0.021604123839437214, -0.013162680889718684, -0.01678344464520518, -0.004296972292525528, -0.004905177119258366  …  -6.627484313750857e-7, -1.5101211052488287e-7, -7.52150227113502e-7, -5.664039430184978e-7, -4.2735612965806707e-7, -3.2682047950118643e-7, -9.879242267200632e-7, -5.930152520612125e-7, -3.2459795367496876e-7, 1.9253629660874997e-9], 7)"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x, iter, k = pcg_quadratic_tol(A, b, x0, M, true)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pcg_quadratic (generic function with 3 methods)"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "function pcg_quadratic(A:: Matrix, b:: Vector, x0:: Vector, M:: Matrix,\n",
    "                       trace:: Bool = false, tol = 1e-8)\n",
    "    n = length(x0)\n",
    "    x = x0\n",
    "    if (trace)\n",
    "        iter = [ x ]\n",
    "    end\n",
    "    g = b+A*x\n",
    "    v = M\\g\n",
    "    d = -v\n",
    "    k = 0\n",
    "    \n",
    "    tol2 = tol*tol\n",
    "\n",
    "    β = 0.0\n",
    "\n",
    "    gv = dot(g,v)\n",
    "    while ((gv > tol2) && (k <= n))\n",
    "#    while ((dot(g,g) > tol2) && (k <= n))\n",
    "        Ad = A*d\n",
    "        normd = dot(d,Ad)\n",
    "        #gv = dot(g,v)\n",
    "        α = gv/normd\n",
    "        x += α*d\n",
    "        if (trace)\n",
    "            iter = [ iter; x ]\n",
    "        end\n",
    "        g += α*Ad\n",
    "        v = M\\g\n",
    "        gvold = gv\n",
    "        gv = dot(g,v)\n",
    "        β = gv/gvold\n",
    "        d = -v+β*d\n",
    "        k += 1\n",
    "    end\n",
    "\n",
    "    if (trace)\n",
    "        iter = [ iter; x ]\n",
    "        return x, iter, k\n",
    "    end\n",
    "\n",
    "    return x, k\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Adapted from https://en.wikipedia.org/wiki/Incomplete_Cholesky_factorization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ichol (generic function with 1 method)"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "function ichol(A:: Matrix)\n",
    "\n",
    "    n = size(A,1)\n",
    "    C = zeros(n,n)+I\n",
    "    \n",
    "    for k=1:n\n",
    "        C[k,k] = sqrt(A[k,k])\n",
    "        for i=(k+1):n\n",
    "            if (A[i,k] != 0)\n",
    "                C[i,k] = A[i,k]/A[k,k]    \n",
    "            end\n",
    "        end\n",
    "        for j=(k+1):n\n",
    "            for i=j:n\n",
    "                if (A[i,j] != 0)\n",
    "                    C[i,j] = A[i,j]-A[i,k]*A[j,k]\n",
    "                end\n",
    "            end\n",
    "        end\n",
    "    end\n",
    "\n",
    "    return C\n",
    "end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1000×1000 LowerTriangular{Float64,Array{Float64,2}}:\n",
       " 1.73205    ⋅         ⋅          ⋅          …     ⋅           ⋅            ⋅ \n",
       " 0.57735   2.38048    ⋅          ⋅                ⋅           ⋅            ⋅ \n",
       " 0.0       0.420084  3.28991     ⋅                ⋅           ⋅            ⋅ \n",
       " 0.0       0.0       0.303959   4.23174           ⋅           ⋅            ⋅ \n",
       " 0.0       0.0       0.0        0.23631           ⋅           ⋅            ⋅ \n",
       " 0.0       0.0       0.0        0.0         …     ⋅           ⋅            ⋅ \n",
       " 0.0       0.0       0.0        0.0               ⋅           ⋅            ⋅ \n",
       " 0.0       0.0       0.0        0.0               ⋅           ⋅            ⋅ \n",
       " 0.0       0.0       0.0        0.0               ⋅           ⋅            ⋅ \n",
       " 0.0       0.0       0.0        0.0               ⋅           ⋅            ⋅ \n",
       " 0.0       0.0       0.0        0.0         …     ⋅           ⋅            ⋅ \n",
       " 0.0       0.0       0.0        0.0               ⋅           ⋅            ⋅ \n",
       " 0.0       0.0       0.0        0.0               ⋅           ⋅            ⋅ \n",
       " ⋮                                          ⋱                                \n",
       " 0.0       0.0       0.0        0.0               ⋅           ⋅            ⋅ \n",
       " 0.0       0.0       0.0        0.0               ⋅           ⋅            ⋅ \n",
       " 0.0       0.0       0.0        0.0         …     ⋅           ⋅            ⋅ \n",
       " 0.0       0.0       0.0        0.0               ⋅           ⋅            ⋅ \n",
       " 0.0       0.0       0.0        0.0               ⋅           ⋅            ⋅ \n",
       " 0.0       0.0       0.0        0.0               ⋅           ⋅            ⋅ \n",
       " 0.0       0.0       0.0        0.0               ⋅           ⋅            ⋅ \n",
       " 0.0       0.0       0.0        0.0         …     ⋅           ⋅            ⋅ \n",
       " 0.0       0.0       0.0        0.0               ⋅           ⋅            ⋅ \n",
       " 0.0       0.0       0.0        0.0            998.001        ⋅            ⋅ \n",
       " 0.0       0.0       0.0        0.0              0.001002  999.001         ⋅ \n",
       " 0.57735  -0.140028  0.01788   -0.00128429       0.0         0.001001  1000.0"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C = cholesky(A)\n",
    "C.L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1000×1000 Array{Float64,2}:\n",
       " 3.0   1.0          0.0          …       0.0       0.0   1.0        \n",
       " 1.0   6.0          1.0                  0.0       0.0   0.0        \n",
       " 0.0   1.0         11.0                  0.0       0.0   0.0        \n",
       " 0.0   0.0          1.0                  0.0       0.0  -8.67362e-19\n",
       " 0.0   0.0          0.0                  0.0       0.0   0.0        \n",
       " 0.0   0.0          0.0          …       0.0       0.0   0.0        \n",
       " 0.0   0.0          0.0                  0.0       0.0   0.0        \n",
       " 0.0   0.0          0.0                  0.0       0.0   0.0        \n",
       " 0.0   0.0          0.0                  0.0       0.0   0.0        \n",
       " 0.0   0.0          0.0                  0.0       0.0   0.0        \n",
       " 0.0   0.0          0.0          …       0.0       0.0   0.0        \n",
       " 0.0   0.0          0.0                  0.0       0.0   1.2326e-32 \n",
       " 0.0   0.0          0.0                  0.0       0.0   0.0        \n",
       " ⋮                               ⋱                                  \n",
       " 0.0   0.0          0.0                  0.0       0.0   0.0        \n",
       " 0.0   0.0          0.0                  0.0       0.0   0.0        \n",
       " 0.0   0.0          0.0          …       0.0       0.0   0.0        \n",
       " 0.0   0.0          0.0                  0.0       0.0   0.0        \n",
       " 0.0   0.0          0.0                  0.0       0.0   0.0        \n",
       " 0.0   0.0          0.0                  0.0       0.0   0.0        \n",
       " 0.0   0.0          0.0                  0.0       0.0   0.0        \n",
       " 0.0   0.0          0.0          …       0.0       0.0   0.0        \n",
       " 0.0   0.0          0.0                  1.0       0.0   0.0        \n",
       " 0.0   0.0          0.0             996006.0       1.0   0.0        \n",
       " 0.0   0.0          0.0                  1.0  998003.0   1.0        \n",
       " 1.0  -1.0935e-17   1.97907e-18          0.0       1.0   1.0e6      "
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "M = C.L*C.U"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([-0.302872935477591, 0.0628120785048972, -0.08044752710531945, -0.04468954807659282, -0.02010717159043811, -0.012567711996097228, -0.016941340433343832, -0.004090090645152937, -0.004900069632543694, -0.008087212140121163  …  -6.627484294845439e-7, -1.5101211009477288e-7, -7.521502249676146e-7, -5.664039414029092e-7, -4.27356128439094e-7, -3.268204785693923e-7, -9.879242239014944e-7, -5.930152503697571e-7, -3.2459795274953857e-7, 1.9253664927258085e-9], Any[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], -0.302872935477591, 0.0628120785048972, -0.08044752710531945, -0.04468954807659282, -0.02010717159043811, -0.012567711996097228, -0.016941340433343832, -0.004090090645152937, -0.004900069632543694  …  -6.627484294845439e-7, -1.5101211009477288e-7, -7.521502249676146e-7, -5.664039414029092e-7, -4.27356128439094e-7, -3.268204785693923e-7, -9.879242239014944e-7, -5.930152503697571e-7, -3.2459795274953857e-7, 1.9253664927258085e-9], 1)"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x, iter, k = pcg_quadratic(A, b, x0, M, true)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1000×1000 Array{Float64,2}:\n",
       " 1.73205   0.0       0.0        …    0.0           0.0          0.0\n",
       " 0.333333  2.44949   0.0             0.0           0.0          0.0\n",
       " 0.0       0.166667  3.31662         0.0           0.0          0.0\n",
       " 0.0       0.0       0.0909091       0.0           0.0          0.0\n",
       " 0.0       0.0       0.0             0.0           0.0          0.0\n",
       " 0.0       0.0       0.0        …    0.0           0.0          0.0\n",
       " 0.0       0.0       0.0             0.0           0.0          0.0\n",
       " 0.0       0.0       0.0             0.0           0.0          0.0\n",
       " 0.0       0.0       0.0             0.0           0.0          0.0\n",
       " 0.0       0.0       0.0             0.0           0.0          0.0\n",
       " 0.0       0.0       0.0        …    0.0           0.0          0.0\n",
       " 0.0       0.0       0.0             0.0           0.0          0.0\n",
       " 0.0       0.0       0.0             0.0           0.0          0.0\n",
       " ⋮                              ⋱                                  \n",
       " 0.0       0.0       0.0             0.0           0.0          0.0\n",
       " 0.0       0.0       0.0             0.0           0.0          0.0\n",
       " 0.0       0.0       0.0        …    0.0           0.0          0.0\n",
       " 0.0       0.0       0.0             0.0           0.0          0.0\n",
       " 0.0       0.0       0.0             0.0           0.0          0.0\n",
       " 0.0       0.0       0.0             0.0           0.0          0.0\n",
       " 0.0       0.0       0.0             0.0           0.0          0.0\n",
       " 0.0       0.0       0.0        …    0.0           0.0          0.0\n",
       " 0.0       0.0       0.0             0.0           0.0          0.0\n",
       " 0.0       0.0       0.0           998.001         0.0          0.0\n",
       " 0.0       0.0       0.0             1.00401e-6  999.001        0.0\n",
       " 0.333333  0.0       0.0             0.0           1.002e-6  1000.0"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C = ichol(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1000×1000 Array{Float64,2}:\n",
       " 3.0      0.57735    0.0        0.0       …       0.0       0.57735 \n",
       " 0.57735  6.11111    0.408248   0.0               0.0       0.111111\n",
       " 0.0      0.408248  11.0278     0.301511          0.0       0.0     \n",
       " 0.0      0.0        0.301511  18.0083            0.0       0.0     \n",
       " 0.0      0.0        0.0        0.235702          0.0       0.0     \n",
       " 0.0      0.0        0.0        0.0       …       0.0       0.0     \n",
       " 0.0      0.0        0.0        0.0               0.0       0.0     \n",
       " 0.0      0.0        0.0        0.0               0.0       0.0     \n",
       " 0.0      0.0        0.0        0.0               0.0       0.0     \n",
       " 0.0      0.0        0.0        0.0               0.0       0.0     \n",
       " 0.0      0.0        0.0        0.0       …       0.0       0.0     \n",
       " 0.0      0.0        0.0        0.0               0.0       0.0     \n",
       " 0.0      0.0        0.0        0.0               0.0       0.0     \n",
       " ⋮                                        ⋱                         \n",
       " 0.0      0.0        0.0        0.0               0.0       0.0     \n",
       " 0.0      0.0        0.0        0.0               0.0       0.0     \n",
       " 0.0      0.0        0.0        0.0       …       0.0       0.0     \n",
       " 0.0      0.0        0.0        0.0               0.0       0.0     \n",
       " 0.0      0.0        0.0        0.0               0.0       0.0     \n",
       " 0.0      0.0        0.0        0.0               0.0       0.0     \n",
       " 0.0      0.0        0.0        0.0               0.0       0.0     \n",
       " 0.0      0.0        0.0        0.0       …       0.0       0.0     \n",
       " 0.0      0.0        0.0        0.0               0.0       0.0     \n",
       " 0.0      0.0        0.0        0.0               0.001002  0.0     \n",
       " 0.0      0.0        0.0        0.0          998003.0       0.001001\n",
       " 0.57735  0.111111   0.0        0.0               0.001001  1.0e6   "
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "M=C*C'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([-0.3028729348712248, 0.06281207863588409, -0.08044752743781927, -0.0446895483119123, -0.020107171544602988, -0.012567712048556683, -0.01694134006428405, -0.004090090604532912, -0.004900069536930813, -0.008087212162320046  …  -6.627484390001745e-7, -1.510121122626337e-7, -7.521502357670275e-7, -5.66403949535178e-7, -4.2735613457495256e-7, -3.268204832615723e-7, -9.87924238086172e-7, -5.930152588840834e-7, -3.245979574089855e-7, 1.9253587148019873e-9], Any[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0  …  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], -0.28970488296005, 0.031579682453543666, -0.07890400459005509, -0.048947519530897765, -0.021835610398601116, -0.013438943873607213, -0.017316518104116798, -0.004401349104935594, -0.005059666033730756  …  -6.627484390001745e-7, -1.510121122626337e-7, -7.521502357670275e-7, -5.66403949535178e-7, -4.2735613457495256e-7, -3.268204832615723e-7, -9.87924238086172e-7, -5.930152588840834e-7, -3.245979574089855e-7, 1.9253587148019873e-9], 6)"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x, iter, k = pcg_quadratic(A, b, x0, M, true)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "An efficient implementation would make use of sparse matrices and specific functions to compute v."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "302408.83092529565"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for i = 1:n-1\n",
    "    A[i,i+1] = 1\n",
    "    A[i+1,i] = 1\n",
    "end\n",
    "for i = 1:10000\n",
    "    j = Int(ceil(rand()*1000))\n",
    "    A[n,j] = 5\n",
    "    A[j,n] = 5\n",
    "end\n",
    "cond(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1000×1000 Array{Float64,2}:\n",
       " 3.0  1.0   0.0   0.0   0.0   0.0   0.0  …       0.0       0.0       0.0  5.0\n",
       " 1.0  6.0   1.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  5.0\n",
       " 0.0  1.0  11.0   1.0   0.0   0.0   0.0          0.0       0.0       0.0  5.0\n",
       " 0.0  0.0   1.0  18.0   1.0   0.0   0.0          0.0       0.0       0.0  5.0\n",
       " 0.0  0.0   0.0   1.0  27.0   1.0   0.0          0.0       0.0       0.0  5.0\n",
       " 0.0  0.0   0.0   0.0   1.0  38.0   1.0  …       0.0       0.0       0.0  5.0\n",
       " 0.0  0.0   0.0   0.0   0.0   1.0  51.0          0.0       0.0       0.0  5.0\n",
       " 0.0  0.0   0.0   0.0   0.0   0.0   1.0          0.0       0.0       0.0  5.0\n",
       " 0.0  0.0   0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  5.0\n",
       " 0.0  0.0   0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  5.0\n",
       " 0.0  0.0   0.0   0.0   0.0   0.0   0.0  …       0.0       0.0       0.0  5.0\n",
       " 0.0  0.0   0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  5.0\n",
       " 0.0  0.0   0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  5.0\n",
       " ⋮                            ⋮          ⋱                                   \n",
       " 0.0  0.0   0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  5.0\n",
       " 0.0  0.0   0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  5.0\n",
       " 0.0  0.0   0.0   0.0   0.0   0.0   0.0  …       0.0       0.0       0.0  5.0\n",
       " 0.0  0.0   0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  5.0\n",
       " 0.0  0.0   0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  5.0\n",
       " 0.0  0.0   0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  5.0\n",
       " 0.0  0.0   0.0   0.0   0.0   0.0   0.0          0.0       0.0       0.0  5.0\n",
       " 0.0  0.0   0.0   0.0   0.0   0.0   0.0  …       1.0       0.0       0.0  5.0\n",
       " 0.0  0.0   0.0   0.0   0.0   0.0   0.0     994011.0       1.0       0.0  5.0\n",
       " 0.0  0.0   0.0   0.0   0.0   0.0   0.0          1.0  996006.0       1.0  5.0\n",
       " 0.0  0.0   0.0   0.0   0.0   0.0   0.0          0.0       1.0  998003.0  5.0\n",
       " 5.0  5.0   5.0   5.0   5.0   5.0   5.0          5.0       5.0       5.0  5.0"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "ename": "PosDefException",
     "evalue": "PosDefException: matrix is not positive definite; Cholesky factorization failed.",
     "output_type": "error",
     "traceback": [
      "PosDefException: matrix is not positive definite; Cholesky factorization failed.",
      "",
      "Stacktrace:",
      " [1] checkpositivedefinite at D:\\buildbot\\worker\\package_win64\\build\\usr\\share\\julia\\stdlib\\v1.3\\LinearAlgebra\\src\\factorization.jl:18 [inlined]",
      " [2] #cholesky!#124(::Bool, ::typeof(cholesky!), ::Hermitian{Float64,Array{Float64,2}}, ::Val{false}) at D:\\buildbot\\worker\\package_win64\\build\\usr\\share\\julia\\stdlib\\v1.3\\LinearAlgebra\\src\\cholesky.jl:226",
      " [3] #cholesky! at .\\none:0 [inlined]",
      " [4] #cholesky!#125(::Bool, ::typeof(cholesky!), ::Array{Float64,2}, ::Val{false}) at D:\\buildbot\\worker\\package_win64\\build\\usr\\share\\julia\\stdlib\\v1.3\\LinearAlgebra\\src\\cholesky.jl:258",
      " [5] #cholesky#129 at .\\none:0 [inlined]",
      " [6] cholesky at D:\\buildbot\\worker\\package_win64\\build\\usr\\share\\julia\\stdlib\\v1.3\\LinearAlgebra\\src\\cholesky.jl:348 [inlined] (repeats 2 times)",
      " [7] top-level scope at In[111]:1"
     ]
    }
   ],
   "source": [
    "C = cholesky(A)\n",
    "C.L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "@webio": {
   "lastCommId": null,
   "lastKernelId": null
  },
  "kernelspec": {
   "display_name": "Julia 1.3.1",
   "language": "julia",
   "name": "julia-1.3"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "1.3.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
